<?php
/**
 * User controller.
 *
 * @category Zend
 * @package User
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 * @license GPL v3
 */

class UserController extends Zend_Controller_Action
{
    /**
     * Log out action
     */
    public function logoutAction()
    {
        Disputeo_Auth::signOut();
        $this->_redirect('/');
    }
    
    /**
     * Login action
     */
    public function loginAction()
    {
        $post = $this->_request->getPost();
        $category = $this->_request->post;
        $referer = $category ? '/disputeo/post?cat=' . $category : $this->_request->getHeader('referer');
        $form = new Application_Form_Login();
        $form->populate(array('referer' => $referer));
        
        if ($this->_request->isPost() && $form->isValid($post)) {
            $data = array(
                'userName' => $form->username->getValue(),
                'password' => $form->password->getValue()
            );
            if (Disputeo_Auth::isValid($data)) {
                $remember = $form->rememberme->getValue();
                $this->_request->session->persist($remember);
                $this->_redirect($form->referer->getValue());
            }
            $form->authFailed();
        }
        
        $this->view->form = $form;
    }
    
    /**
     * Signup action
     */
    public function signupAction()
    {
        $form = new Application_Form_Signup();
        $post = $this->_request->getPost();
        
        if ($this->_request->isPost() && $form->isValid($post)) {
            $data = array(
                'userName' => $form->username->getValue(),
                'password' => $form->password->getValue(),
                'email' => $form->email->getValue()
            );
            $user = Application_Model_User::create($data);
            Disputeo_Auth::authorise($user);
            $remember = $form->rememberme->getValue();
            $this->_request->session->persist($remember);
            $this->_helper->FlashMessenger->addMessage('newSignup');
            $this->_redirect('/');
        }
        
        $this->view->form = $form;
    }
}

